package com.cn.hotel.service.impl;

import com.cn.hotel.dao.IRoomDao;
import com.cn.hotel.dao.Impl.RoomImpl;
import com.cn.hotel.model.RoomModel;
import com.cn.hotel.service.IRoomService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class RoomServiceImpl implements IRoomService {
    /**
     * 添加
     *
     * @param roomModel
     * @return
     */
    @Override
    public int add(RoomModel roomModel) {
        IRoomDao iRoomDao = new RoomImpl();
        int result = iRoomDao.add(roomModel);
        return result;
    }

    /**
     * 查询
     *
     * @param roomModel
     * @return
     */
    @Override
    public List<RoomModel> findAll(RoomModel roomModel) {
        IRoomDao iRoomDao = new RoomImpl();
        List<RoomModel> list = iRoomDao.findAll(roomModel);
        return list;
    }

    /**
     * 得到总条树
     *
     * @param roomModel
     * @return
     */
    @Override
    public int getCount(RoomModel roomModel) {
        IRoomDao iRoomDao = new RoomImpl();
        return iRoomDao.getCount(roomModel);
    }

    /**
     * 通过房间编号查询信息  然后给编辑页面赋值所点击的信息
     *
     * @param aa
     * @return
     */
    @Override
    public RoomModel findByRoomId(Integer aa) {
        IRoomDao iRoomDao = new RoomImpl();
        return iRoomDao.findByRoomId(aa);
    }

    /**
     * 根据id删除
     *
     * @param temp
     * @return
     */
    @Override
    public int delete(Integer temp) {
        IRoomDao iRoomDao = new RoomImpl();
        return iRoomDao.delete(temp);
    }

    /**
     * 编辑提交数据
     *
     * @param roomModel
     * @return
     */
    @Override
    public int edit(RoomModel roomModel) {
        IRoomDao iRoomDao = new RoomImpl();
        return iRoomDao.edit(roomModel);
    }

    //-------------------前端调值👇👇👇👇👇👇👇👇👇👇👇👇----------------------------
    @Override
    public List<RoomModel> getRoomModel() {
        IRoomDao iRoomDao = new RoomImpl();
        return iRoomDao.getRoomModel();
    }

    /**
     * 网站首页调取
     *
     * @return
     */
    @Override
    public List<RoomModel> getRoomModelList() {
        IRoomDao iRoomDao = new RoomImpl();
        return iRoomDao.getRoomModelList();
    }

    /**
     * 获取指定数量的数据
     *
     * @return
     */
    @Override
    public List<RoomModel> getAppointRoomModel() {
        IRoomDao iRoomDao = new RoomImpl();
        return iRoomDao.getAppointRoomModel();
    }

    /**
     * 获取首页的三张图片
     *
     * @return
     */
    @Override
    public List<RoomModel> getThree() {
        IRoomDao iRoomDao = new RoomImpl();
        List<RoomModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
//        RoomModel roomModel = new RoomModel();
        if (jedis.exists("home_roomPhoto")) {
            List<String> home_roomPhoto = jedis.lrange("home_roomPhoto", 0, -1);
            List<String> home_backGroundImg = jedis.lrange("home_backGroundImg", 0, -1);
            List<String> home_roomTitle = jedis.lrange("home_roomTitle", 0, -1);
            for (int i = 0; i < home_roomPhoto.size(); i++) {
                //赋值
                RoomModel model = new RoomModel();
                model.setRoomPhoto(home_roomPhoto.get(i));
                model.setHomeBackGroud(home_backGroundImg.get(i));
                model.setHomeBackTitle(home_roomTitle.get(i));
                list.add(model);
            }
        } else {
            /**
             * ！！！后台暂时没写控制这三张图片的增删改，后续需要在增删改取加判断 如果进行了增删改操作，
             *      则删除jedis里卖弄的数据进行重新赋值
             */
            list = iRoomDao.getThree();
            for (int i = 0; i < list.size(); i++) {
                jedis.rpush("home_roomPhoto",list.get(i).getRoomPhoto());
                jedis.rpush("home_backGroundImg",list.get(i).getHomeBackGroud());
                jedis.rpush("home_roomTitle",list.get(i).getHomeBackTitle());
            }
        }
        return list;
    }
}
